from pyramid.config import Configurator
from pyramid.response import Response, FileResponse
from pyramid.events import NewRequest


from med.views.appointments import get_appointments_view
from med.views.appointment import get_appointment_view
from med.views.current_user import get_current_user_view
from med.views.download import get_download_file
from med.views.entry import get_entry_view
from med.views.filters import get_filters_view
from med.views.record import get_record_view
from med.views.search import get_search_view
from med.views.users import get_users_view


def home(request):
    return Response("OK")


def appointments(request):
    return get_appointments_view()


def download(request):
    filepath, filename, content_type = get_download_file(
        file_id=request.params.get("id", None),
        filename=request.params.get("filename", None),
        repo=request.params.get("repo", None),
        fkey=request.params.get("fkey", None),
    )
    response = FileResponse(filepath, request=request, content_type=content_type)
    response.headers["Content-Disposition"] = f'attachment; filename="{filename}"'
    return response


def current_user(request):
    return get_current_user_view()


def entry(request):
    return get_entry_view(entry_id=request.params.get("id", None))


def filters(request):
    return get_filters_view()


def record(request):
    return get_record_view(record_id=request.params.get("id", None))


def search(request):
    return get_search_view(search_input=request.params.get("q", None))


def users(request):
    return get_users_view()


def add_cors_headers_response_callback(event):
    def cors_headers(request, response):
        response.headers.update(
            {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "POST,GET,DELETE,PUT,OPTIONS",
                "Access-Control-Allow-Headers": "Origin, Content-Type, Accept, Authorization",
                "Access-Control-Allow-Credentials": "true",
                "Access-Control-Max-Age": "1728000",
            }
        )

    event.request.add_response_callback(cors_headers)


def get_app(enable_cors=False):
    with Configurator() as config:

        if enable_cors:
            config.add_subscriber(add_cors_headers_response_callback, NewRequest)

        config.add_route(home.__name__, "/")
        config.add_view(home, route_name=home.__name__)

        config.add_route(appointments.__name__, "/api/appointments")
        config.add_view(
            appointments,
            route_name=appointments.__name__,
            renderer="json",
        )

        config.add_route(download.__name__, "/api/download")
        config.add_view(
            download,
            route_name=download.__name__,
            renderer="json",
        )

        config.add_route(current_user.__name__, "/api/current_user")
        config.add_view(
            current_user,
            route_name=current_user.__name__,
            renderer="json",
        )

        config.add_route(entry.__name__, "/api/entry")
        config.add_view(
            entry,
            route_name=entry.__name__,
            renderer="json",
        )

        config.add_route(filters.__name__, "/api/filters")
        config.add_view(
            filters,
            route_name=filters.__name__,
            renderer="json",
        )

        config.add_route(record.__name__, "/api/record")
        config.add_view(
            record,
            route_name=record.__name__,
            renderer="json",
        )

        config.add_route(search.__name__, "/api/search")
        config.add_view(
            search,
            route_name=search.__name__,
            renderer="json",
        )

        config.add_route(users.__name__, "/api/users")
        config.add_view(
            users,
            route_name=users.__name__,
            renderer="json",
        )

        return config.make_wsgi_app()
